package com.ruoyi.bysj.service.impl;

import java.util.List;

import com.ruoyi.bysj.domain.SysAppointment;
import com.ruoyi.bysj.domain.SysOrder;
import com.ruoyi.bysj.mapper.SysAppointmentMapper;
import com.ruoyi.bysj.mapper.SysOrderMapper;
import com.ruoyi.common.utils.DateUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.bysj.mapper.SysReceptionMapper;
import com.ruoyi.bysj.domain.SysReception;
import com.ruoyi.bysj.service.ISysReceptionService;

/**
 * 接待车主Service业务层处理
 * 
 * @author laow
 * @date 2021-10-25
 */
@Service
public class SysReceptionServiceImpl implements ISysReceptionService 
{
    @Autowired
    private SysReceptionMapper sysReceptionMapper;

    @Autowired
    private SysOrderMapper sysOrderMapper;

    @Autowired
    private SysAppointmentMapper sysAppointmentMapper;

    /**
     * 查询接待车主
     * 
     * @param receptionId 接待车主主键
     * @return 接待车主
     */
    @Override
    public SysReception selectSysReceptionByReceptionId(Long receptionId)
    {
        return sysReceptionMapper.selectSysReceptionByReceptionId(receptionId);
    }

    /**
     * 查询接待车主列表
     * 
     * @param sysReception 接待车主
     * @return 接待车主
     */
    @Override
    public List<SysReception> selectSysReceptionList(SysReception sysReception)
    {
        return sysReceptionMapper.selectSysReceptionList(sysReception);
    }

    /**
     * 新增接待车主
     * 
     * @param sysReception 接待车主
     * @return 结果
     */
    @Override
    public int insertSysReception(SysReception sysReception)
    {
        SysOrder sysOrder = sysOrderMapper.selectSysOrderByOrderId(sysReception.getOrderId());
        sysOrder.setOrderReceiver(sysReception.getReceptionName());
        sysOrder.setCarId(sysReception.getCarId());
        String OrderState = sysOrder.getOrderState();
        if ( OrderState.equals("1")){
            sysOrder.setOrderState("2");
        }
        else if ( OrderState.equals("4")){
            sysOrder.setOrderState("5");
        }
        SysAppointment sysAppointment = sysAppointmentMapper.selectSysAppointmentByAppointmentId(sysOrder.getAppointmentId());
        sysAppointment.setAppointmentState("3");
        sysAppointmentMapper.updateSysAppointment(sysAppointment);
        sysOrderMapper.updateSysOrder(sysOrder);
        sysReception.setAppointmentName(sysOrder.getAppointmentName());
        sysReception.setReceptionTime(DateUtils.getNowDate());
        return sysReceptionMapper.insertSysReception(sysReception);
    }

    @Override
    public int insertSysReceptionByName(SysReception sysReception)
    {
        SysAppointment sysAppointment = sysAppointmentMapper.selectSysAppointmentByAppointmentId(sysReception.getAppointmentId());
        sysAppointment.setAppointmentState("3");
        sysAppointmentMapper.updateSysAppointment(sysAppointment);
        SysOrder sysOrder = sysOrderMapper.selectSysOrderByAppointmentId(sysReception.getAppointmentId());
        sysOrder.setOrderReceiver(sysReception.getReceptionName());
        sysOrder.setCarId(sysReception.getCarId());
        String OrderState = sysOrder.getOrderState();
        sysOrder.setOrderState("2");
        sysOrderMapper.updateSysOrder(sysOrder);
        sysReception.setAppointmentName(sysOrder.getAppointmentName());
        sysReception.setReceptionTime(DateUtils.getNowDate());
        return sysReceptionMapper.insertSysReception(sysReception);
    }
    /**
     * 修改接待车主
     * 
     * @param sysReception 接待车主
     * @return 结果
     */
    @Override
    public int updateSysReception(SysReception sysReception)
    {
        return sysReceptionMapper.updateSysReception(sysReception);
    }

    /**
     * 批量删除接待车主
     * 
     * @param receptionIds 需要删除的接待车主主键
     * @return 结果
     */
    @Override
    public int deleteSysReceptionByReceptionIds(Long[] receptionIds)
    {
        return sysReceptionMapper.deleteSysReceptionByReceptionIds(receptionIds);
    }

    /**
     * 删除接待车主信息
     * 
     * @param receptionId 接待车主主键
     * @return 结果
     */
    @Override
    public int deleteSysReceptionByReceptionId(Long receptionId)
    {
        return sysReceptionMapper.deleteSysReceptionByReceptionId(receptionId);
    }
}
